package com.sgb.kjwl.view.ui.activity.self.authentication

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityOrganizationDetailBinding
import com.sgb.kjwl.di.self.DaggerSelfComponent
import com.sgb.kjwl.di.self.SelfModule
import com.sgb.kjwl.model.entity.self.OrganizationAuthEntity
import com.sgb.kjwl.model.entity.self.TeamAuthEntity
import com.swgk.core.event.EventConstant
import com.sgb.kjwl.view.event.self.OrganizationAuthEvent
import com.sgb.kjwl.viewmodel.self.authentication.OrganizationAuthModel
import com.swgk.core.base.di.AppComponent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import javax.inject.Inject

class OrganizationDetailActivity : AppActivity(){
    private lateinit var mBinding : ActivityOrganizationDetailBinding
    private var mEntity : OrganizationAuthEntity ?= null
    private var mDetailId : Int ?= -1

    private lateinit var mRealNameUserDTO : TeamAuthEntity.RealNameUserDTO;//如果未认证

    @Inject
    lateinit var mViewModel: OrganizationAuthModel

    companion object {
        const val KEY_ENTITY = "key_entity"
        fun launch(context: Context?, entity : OrganizationAuthEntity) {
            val intent = Intent(context, OrganizationDetailActivity::class.java)
            intent.putExtra(KEY_ENTITY, entity)
            context!!.startActivity(intent)
        }
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        super.setupActivityComponent(appComponent)
        DaggerSelfComponent.builder().appComponent(appComponent)
            .selfModule(SelfModule()).build().inject(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }


    override fun initView() {
        super.initView()
        if (intent != null){
            mEntity = intent.getSerializableExtra(KEY_ENTITY) as OrganizationAuthEntity?
        }
        if (mEntity == null){
            finish()
            return
        }
        if(mEntity!!.realNameUserDTO == null){
            mRealNameUserDTO = TeamAuthEntity.RealNameUserDTO()
            mRealNameUserDTO.identityCard = mEntity!!.card
            mRealNameUserDTO.phone = mEntity!!.userPhone
            mRealNameUserDTO.realName = mEntity!!.userName
            mEntity!!.realNameUserDTO = mRealNameUserDTO
        }
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_organization_detail)
        mBinding.include.ivBack.setOnClickListener { finish() }
        mBinding.tvAgainAuth.setOnClickListener {
            OrganizationAuthActivity.launch(this, mEntity)
        }
        mBinding.include.titleView.text = "认证详情"
        mViewModel.mOrganizationEntity.observe(this, {
            if (it != null){
                mBinding.viewModel = it
                renderHeadView(it)
            }
        })
    }

    override fun init() {
        super.init()
        mViewModel.getOrganizationDetailByNo(this, mEntity!!.organizationNo)
    }

    private fun renderHeadView(mEntity: OrganizationAuthEntity) {
        if (mEntity.status == 3){
            mBinding.ivStatus.setImageResource(R.mipmap.auth_success)
            mBinding.tvStatus.text = "认证成功"
            mBinding.tvFailReason.visibility = View.GONE
            mBinding.tvAgainAuth.visibility = View.GONE
        } else if (mEntity.status == 2) {
            mBinding.ivStatus.setImageResource(R.mipmap.authen_fail)
            mBinding.tvStatus.text = "认证失败"
            mBinding.tvFailReason.visibility = View.VISIBLE
            mBinding.tvAgainAuth.visibility = View.VISIBLE
            mBinding.tvFailReason.text = if(TextUtils.isEmpty(mEntity.reason)) "" else "失败原因：" + mEntity.reason
        } else {//审核中
            mBinding.ivStatus.setImageResource(R.mipmap.authen_ing)
            mBinding.tvStatus.text = "审核中"
            mBinding.tvFailReason.visibility = View.GONE
            mBinding.tvAgainAuth.visibility = View.GONE
        }
        if (TextUtils.isEmpty(mEntity.getLongTimeStr())){
            mBinding.itvItemEffective.content = mEntity.getCreateTime() + "至" + mEntity.getValidityTime()
        } else {
            mBinding.itvItemEffective.content = mEntity.getCreateTime() + "至" + mEntity.getLongTimeStr()
        }
        Glide.with(this).load(mEntity.codeCertificatePic).into(mBinding.ivPic)
        mBinding.itvItemCreateTime.content = mEntity.getCreateTime()
        mBinding.tvDetailAddress.post {
            mBinding.tvDetailAddress.gravity = if (mBinding.tvDetailAddress.lineCount > 1) Gravity.LEFT else Gravity.RIGHT
        }
        mBinding.tvBusinessScope.post {
            mBinding.tvBusinessScope.gravity = if (mBinding.tvBusinessScope.lineCount > 1) Gravity.LEFT else Gravity.RIGHT
        }
        }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGetMessage(event: OrganizationAuthEvent) {
        if (event.messageTag == EventConstant.EVENT_ORGANIZATION_AUTH_SUCCESS) {
            finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}